/**
 *
 */
package com.bluetooth.demo;

import android.app.Application;
import android.util.Log;

import com.bluetooth.demo.model.Student;
import com.bluetooth.demo.utils.FileUtils;
import com.lifesense.ble.LsBleInterface;
import com.lifesense.ble.LsBleManager;
import com.lifesense.ble.bean.WeightAppendData;
import com.lifesense.ble.bean.constant.SexType;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.log.LoggerInterceptor;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
public class MyApplication extends Application {

    private static MyApplication instance;
    private Student mStudent;

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        //init LSBluetoothManager
        LsBleManager.getInstance().initialize(getApplicationContext());
        //register bluetooth broadacst receiver
        LsBleManager.getInstance().registerBluetoothBroadcastReceiver(getApplicationContext());

        Log.e("LS-BLE", "LSDevice Bluetooth SDK Version:" + LsBleInterface.BLUETOOTH_SDK_VERSION);
        //for debug mode
        LsBleManager.getInstance().enableWriteDebugMessageToFiles(true, LsBleInterface.PERMISSION_WRITE_LOG_FILE);
        //设置日志路径
        String logPath = FileUtils.createPortraitUrl(getApplicationContext(), "bluetoothTest/log");
        LsBleManager.getInstance().setBlelogFilePath(logPath, "sky", "test");

        //register message service if need
        LsBleManager.getInstance().registerMessageService();

        //for test
        calculateBodyCompositionData(550);


        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .addInterceptor(new LoggerInterceptor("TAG"))
                .connectTimeout(10000L, TimeUnit.MILLISECONDS)
                .readTimeout(10000L, TimeUnit.MILLISECONDS)
                //其他配置
                .build();

        OkHttpUtils.initClient(okHttpClient);
    }

    public static MyApplication getInstance() {
        return instance;
    }

    public Student getStudent() {
        return mStudent;
    }

    public void setStudent(Student student) {
        mStudent = student;
    }

    /**
     * 根据电阻值，计算脂肪数据
     * @param resistance
     */
    private void calculateBodyCompositionData(double resistance) {
//		double resistance=wData.getImpedance();
        double height = 1.65;      // unit M
        double weight = 50;     //  unit kg
        int age = 30;
        SexType gender = SexType.MALE;
        boolean isAthlete = true;
        WeightAppendData bodyComposition = LsBleManager.getInstance().parseAdiposeData(gender, weight, height, age, resistance, isAthlete);
        Log.e("LS-BLE", "body composition >> " + bodyComposition.toString());
        LsBleManager.getInstance().setLogMessage("bodycomposition >> " + bodyComposition.toString());
    }
}
